package com.fgs.common.widget.dialog;

import android.graphics.Point;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.fgs.common.R;
import com.fgs.common.utils.CommonUtil;

/**
 * @author fgs
 * @date 2020/9/10
 * @description
 **/
public class DialogInit {

    public static void init(OperateTipDialog dialog) {
        final OperateTipDialog.Builder builder = dialog.builder;
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(builder.isCancelable);

        dialog.titleTextView = dialog.view.findViewById(R.id.layout_operate_tip_dialog_titleTextView);
        dialog.contentTextView = dialog.view.findViewById(R.id.layout_operate_tip_dialog_contentTextView);
        dialog.contentEditText = dialog.view.findViewById(R.id.layout_operate_tip_dialog_contentEditText);
        dialog.positiveTextView = dialog.view.findViewById(R.id.layout_operate_tip_dialog_positiveTextView);
        dialog.negativeTextView = dialog.view.findViewById(R.id.layout_operate_tip_dialog_negativeTextView);

        dialog.positiveTextView.setVisibility(TextUtils.isEmpty(builder.positiveText) ? View.GONE : View.VISIBLE);
        dialog.negativeTextView.setVisibility(TextUtils.isEmpty(builder.negativeText) ? View.GONE : View.VISIBLE);

        dialog.titleTextView.setText(builder.title);
        if (dialog.contentTextView != null) {
            dialog.contentTextView.setText(builder.content);
        }

        if (dialog.contentEditText != null) {
            dialog.contentEditText.setText(builder.content);
        }

        dialog.positiveTextView.setTag(DialogAction.POSITIVE);
        dialog.positiveTextView.setOnClickListener(dialog);
        dialog.negativeTextView.setTag(DialogAction.NEGATIVE);
        dialog.negativeTextView.setOnClickListener(dialog);

        if (dialog.contentEditText != null) {
            dialog.contentEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (builder.onInputCallback != null) {
                        builder.onInputCallback.onInput(dialog, s);
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }


        dialog.setContentView(dialog.view);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);

        Display display = window.getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);

        final int windowWidth = point.x;
        int windowMargin = (int) CommonUtil.convertDpToPixel(40);
        final int calculateWidth = windowWidth - (windowMargin * 2);


        WindowManager.LayoutParams params = window.getAttributes();
        params.width = calculateWidth;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

    }

    public static int getInflateLayout(OperateTipDialog.Builder builder) {
        if (builder.onInputCallback != null) {
            return R.layout.layout_operate_tip_input_dialog;
        } else {
            return R.layout.layout_operate_tip_dialog;
        }
    }
}
